package fl.ass.service.service.impl;

import java.util.ArrayList;
import java.util.List;

import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.comm.facade.framework.condition.SysRegionQueryCondition;
import cmm.comm.facade.framework.dto.SysRegionDTO;
import cn.fl.project.constant.PrjPrdPaySchMConstant;
import cn.fl.project.dto.PrjPrdPaySchMDTO;
import cn.fl.project.service.IPrjPrdPaySchMApiService;
import fl.ass.facade.api.IAssTrailCarApi;
import fl.ass.facade.dto.AssTrailCarDDTO;
import fl.ass.service.service.IAssTrailCarDService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.Pager;
import cmm.service.core.framework.biz.BaseBizImpl;
import cmm.service.core.framework.dao.IBaseDao;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import fl.ass.facade.condition.AssTrailCarHvQueryCondition;
import fl.ass.facade.constant.InterfaceConstants;
import fl.ass.facade.dto.AssTrailCarHvDTO;
import fl.ass.service.dao.AssTrailCarHvMapper;
import fl.ass.service.po.AssTrailCarHv;
import fl.ass.service.service.IAssTrailCarHvService;
import fl.ass.service.transform.AssTrailCarHvTransform;

/**
 * @Description: 拖车交接单 服务层接口实现
 * @Description: 类中已经实现了简单增删改查操作,可以直接使用,如果有需要重写该方法即可：
 * @Description: 1、add(TVO entity) 2、removeById(Long id) 3、modify(DTO entity) 4、selectById(Long id)	5、searchListPage(Pager page) 7、searchList(DTO dto)   
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AssTrailCarHvServiceImpl extends BaseBizImpl<AssTrailCarHv, AssTrailCarHvDTO, AssTrailCarHvQueryCondition> implements IAssTrailCarHvService {
	
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(IAssTrailCarHvService.class);
	
	@Autowired
	private AssTrailCarHvMapper assTrailCarHvMapper;
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	
	@Autowired
	private ISysOrgApiService sysOrgApiService;//组织架构 API 暴露服务

    @Autowired
    private ISysRegionApiService sysRegionApiService;

    @Autowired
    private IAssTrailCarDService assTrailCarDService;

    @Autowired
    private IPrjPrdPaySchMApiService prjPrdPaySchMApiService;
	
	@Override
	public List<AssTrailCarHvDTO> searchList(AssTrailCarHvQueryCondition condition) throws BaseException {
		if(null == condition) return new ArrayList<AssTrailCarHvDTO>();
		List<AssTrailCarHv> list = this.assTrailCarHvMapper.selectList(condition);
		return AssTrailCarHvTransform.toDTOList(list);
	}
	
	@Override
	public IBaseDao<AssTrailCarHv,AssTrailCarHvQueryCondition> getDao() {
		return this.assTrailCarHvMapper;
	}

	/* (non-Javadoc)
	 * @see fl.ass.service.service.IAssTrailCarHvService#findAssTrailCarHvListByPage(cmm.mid.core.framework.page.Pager)
	 */
	@Override
	public Pager findAssTrailCarHvListByPage(Pager page) {
		List<AssTrailCarHvDTO> list = this.assTrailCarHvMapper.findAssTrailCarHvListByPage(page);
		if(!CollectionUtils.isNotEmpty(list)){
			list = new ArrayList<AssTrailCarHvDTO>();
		}
		SysDictionaryDTO dictionary = null;
		Long dealAmt = null;
		Long zero = new Long(0);
		for(AssTrailCarHvDTO dto : list){
			//处理拖车状态
			if(StringUtils.isNotBlank(dto.getBackStsCd())){
				dictionary = sysDictionaryApiService.getCode(InterfaceConstants.SYS_CODE, InterfaceConstants.ORG_CODE, InterfaceConstants.BACK_STATUS_CD, dto.getBackStsCd());
				if(null != dictionary){
					dto.setBackStsCd(dictionary.getCodeNm());
				}
			}
			//处理处置状态
			if(StringUtils.isNotBlank(dto.getDealStsCd())){
				dictionary = sysDictionaryApiService.getCode(InterfaceConstants.SYS_CODE, InterfaceConstants.ORG_CODE, InterfaceConstants.DEAL_STATUS_CD, dto.getDealStsCd());
				if(null != dictionary){
					dto.setDealStsCd(dictionary.getCodeNm());
				}
			}
			//处理评价状态
			if(StringUtils.isNotBlank(dto.getEvalStatusCd())){
				dictionary = sysDictionaryApiService.getCode(InterfaceConstants.SYS_CODE, InterfaceConstants.ORG_CODE, InterfaceConstants.EVAL_STATUS_CD, dto.getEvalStatusCd());
				if(null != dictionary){
					dto.setEvalStatusCd(dictionary.getCodeNm());
				}
			}
			//处理处置金额
			dealAmt = dto.getDealAmt();
			if(null == dealAmt){
				dealAmt = dto.getSellAmt();
				if(null != dealAmt && dealAmt.compareTo(zero) > 0){
					dto.setDealAmt(dealAmt);
				}else{
					dto.setDealAmt(zero);
				}
			}else{
				if(dealAmt.compareTo(zero) < 1){
					dto.setDealAmt(zero);
				}
			}
		}
		page.setResultList(list);
		return page;
	}

	/* (non-Javadoc)
	 * @see fl.ass.service.service.IAssTrailCarHvService#findPrjPrdInfoByDetailId(java.lang.Long)
	 */
	@Override
	public AssTrailCarHvDTO findPrjPrdInfoByDetailId(Long detailId) {
        AssTrailCarDDTO assTrailCarDDTO = this.assTrailCarDService.selectById(detailId);
        List<PrjPrdPaySchMDTO> prjPrdPaySchMDTOS = this.prjPrdPaySchMApiService.searchTheSamePrdOtherPaySchMList(assTrailCarDDTO.getPaySchId());
        Long wsPaySchId = 0L;
        if (CollectionUtils.isNotEmpty(prjPrdPaySchMDTOS)) {
            for (PrjPrdPaySchMDTO prjPrdPaySchMDTO : prjPrdPaySchMDTOS) {
                if (PrjPrdPaySchMConstant.PAY_SCH_TYP_CD_WS.equals(prjPrdPaySchMDTO.getPaySchTypCd())) {
                    wsPaySchId = prjPrdPaySchMDTO.getId();
                    break;
                }
            }
        }
        AssTrailCarHvDTO dto = this.assTrailCarHvMapper.findPrjPrdInfoByDetailId(detailId, wsPaySchId);
		if(null != dto){
		    /*
              获取省市信息开始
             */
            SysRegionQueryCondition qc = new SysRegionQueryCondition();
            qc.setSysCd("11000");
            qc.setRgnLvl(1);
            qc.setRgnCd(dto.getProvinceCd());
            List<SysRegionDTO> sysRegionProvince = this.sysRegionApiService.searchRegionList(qc);
            if (CollectionUtils.isNotEmpty(sysRegionProvince)) {
                dto.setProvinceCd(sysRegionProvince.get(0).getRgnNm());
            }
            qc.setRgnLvl(2);
            qc.setRgnCd(dto.getCityCd());
            List<SysRegionDTO> sysRegionCity = this.sysRegionApiService.searchRegionList(qc);
            if (CollectionUtils.isNotEmpty(sysRegionCity)) {
                dto.setCityCd(sysRegionCity.get(0).getRgnNm());
            }
            /*
              获取省市信息结束
             */
			SysDictionaryDTO dictionary = null;
			//处理拖车状态
			if(StringUtils.isNotBlank(dto.getPrdType())){
				dictionary = sysDictionaryApiService.getCode(InterfaceConstants.SYS_CODE, InterfaceConstants.ORG_CODE, InterfaceConstants.PRD_TYPE_CD, dto.getPrdType());
				if(null != dictionary){
					dto.setPrdType(dictionary.getCodeNm());
				}
			}
			//获取所属省公司-JL-20181108
			if(StringUtils.isNotBlank(dto.getProvincialCompany())){
				SysOrgDTO org = this.sysOrgApiService.selectSysOrgByOrgCd(dto.getProvincialCompany(), InterfaceConstants.SYS_CODE);
				if(null != org){
					dto.setProvincialCompany(org.getOrgDispNm());
				}
			}
			
		}
		return dto;
	}
	
}

